<?php

namespace Modules\Asset\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Asset\Entities\PackageSku;

use Validator;
use ThrowException;
use Auth;
use Cache;
use DB;
use Asset;

// 已开出物品
class PackageSkuController extends Controller
{
    // 各种状态的数量统计
    public function statusTotal(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();
        $total = [
            'pending' => PackageSku::where('user_id', $user->id)->status('pending')->count(),
            'picked' => PackageSku::where('user_id', $user->id)->status('picked')->count(),
            'return_saled' => PackageSku::where('user_id', $user->id)->status('return_saled')->count(),
            'resaled' => PackageSku::where('user_id', $user->id)->status('resaled')->count(),
            'resale_pending' => PackageSku::where('user_id', $user->id)->status('resale_pending')->count(),
            'locked' => PackageSku::where('user_id', $user->id)->status('locked')->count(),
            'all' => PackageSku::where('user_id', $user->id)->status('all')->count(),
        ];

        return [
            'info' => $total
        ];
    }


    public function index(Request $req)
    {
        $rule = [
            'status' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $options = $req->all();
        
        $options['scopes']['belongUser'] = $user;

        $options['with'] = ['pick_order', 'resale'];
        $options['where_not_in']['status'] = [9];

        $options['only'] = [
            'id',
            'sid',
            'uuid',
            'number',
            'thumb',
            'title',
            'money_price',
            'score_price',
            'node_money_price',
            'node_score_price',
            'total',
            'created_at',
            'union_status',
            'union_status_text',
            'node_type_text',
            'sku_type_text',
            'is_return_saleable',
            'is_resaleable',
            'is_presell',
            'presell_date',
            'pick_order' => [
                'id',
                'uuid'
            ],
            'resale' => [
            	'id',
            	'uuid'
            ],
            'options'
        ];

        $list = PackageSku::getList($options);

        return $list->generateListResponse();
    }

    public function showTry($uuid) 
    {
        $cached  = Cache::get($uuid) ?: ThrowException::Conflict('试玩奖品已失效无法查看~');

        $class = config('map.base_sku_type')[$cached['sku_type']];

        $sku = $class::getEntity([
            'id' => $cached['sku_id'],
            'with' => 'product.ext'
        ]);

        $only = [
            'id',
            'uuid',
            'title',
            'number',
            'thumb',
            'money_price',
            'score_price',
            'union_status',
            'union_status_text',
            'total',
            'activity_type',
            'return_money',
            'return_score',
            'return_redpack',
            'return_saled_at',
            'resaled_at',
            'created_at',
            'sid',
            'options',
            'node_type_text',
            'product' => [
                'id',
                'uuid',
                'title',
                'sub_title',
                'thumb',
                'tags',
                'number',
                'money_price',
                'score_price',
                'ext' => [
                    'images',
                    'page'
                ],
            ]
        ];

        $info = $sku->getInfo($only);

        return [
            'info' => $info
        ];
    }

    // packageSku 详情
    public function show(Request $req, $uuid)
    {
        if (substr($uuid, 0, 4) === 'try_') {
            return $this->showTry($uuid);
        }

        $item = PackageSku::getEntity([
            'uuid' => $uuid,
            'with' => ['product.ext']
        ]);

        $only = [
            'id',
            'uuid',
            'title',
            'number',
            'thumb',
            'money_price',
            'score_price',
            'union_status',
            'union_status_text',
            'total',
            'activity_type',
            'return_money',
            'return_score',
            'return_redpack',
            'return_saled_at',
            'resaled_at',
            'created_at',
            'sid',
            'options',
            'node_type_text',
            'sku_type_text',
            'assets',
            'is_return_saleable',
            'product' => [
                'id',
                'uuid',
                'title',
                'sub_title',
                'thumb',
                'tags',
                'number',
                'money_price',
                'score_price',
                'ext' => [
                    'images',
                    'page'
                ],
            ]
        ];

        $item->assets = Asset::initMultiAsset($item->assets)->getInfo();

        $info = $item->getInfo($only);

        $info['detail_images'] = @$item->base_sku->detail_images;

        return [
            'info' => $info
        ];
    }

    // 锁物品
    public function lock(Request $req, $uuid)
    {
        $user = Auth::requireLoginUser();

        // 奖品
        $packageSku = PackageSku::getEntity([
            'uuid' => $uuid,
            'user_id' => $user->id
        ]);

        if ($packageSku->union_status != 'pending') {
            ThrowException::Conflict('物品不可锁定~');
        }

        DB::beginTransaction();

        $packageSku->update([
            'locked_at' => now(),
            'status' => 7
        ]);

        DB::commit();

        return [];
    }

    // 解锁物品
    public function unlock(Request $req, $uuid)
    {
        $user = Auth::requireLoginUser();

        // 奖品
        $packageSku = PackageSku::getEntity([
            'uuid' => $uuid,
            'user_id' => $user->id
        ]);

        if ($packageSku->union_status != 'locked') {
            ThrowException::Conflict('物品不在锁定中~');
        }

        DB::beginTransaction();

        $packageSku->update([
            'locked_at' => NULL,
            'status' => 1
        ]);

        DB::commit();

        return [];
    }

    public function pickVirtualAsset(Request $req, $uuid)
    {
        $user = Auth::requireLoginUser();

        // 奖品
        $packageSku = PackageSku::getEntity([
            'uuid' => $uuid,
            'user_id' => $user->id
        ]);

        DB::beginTransaction();

        // 标记为提货完成
        $packageSku->pickVirtualAsset() || ThrowException::Conflict('提取失败~');

        DB::commit();

        return [];
    }
}
